/**
 * 对 storage 封装
 * localStorage
 * sessionStorage
 */

 /**
  * name: localstorage key
  * options 
  */

//   person  {  name: '', age: ''  }
//   setStorage('person', { name: 'mm'  })

 const setStorageFun = (name, options, type) => {
    const storage = {
        'sessionStorage': sessionStorage,
        'localStorage': localStorage,
    }
    const currentStorage = storage[type]
    let oldStorage = currentStorage.getItem(name);
    let newStorage = oldStorage;
    if (!oldStorage) {
        try {
            if (typeof options == 'object') {
                // JSON.stringify(options);
                newStorage = JSON.stringify(options);
                currentStorage.setItem(name, newStorage)
                return;
            }

        } catch (err) {
            console.log(err);
        }
        currentStorage.setItem(name, options)
        return;
    }

    // 清空 name
    if (!options) {
        currentStorage.setItem(name, "{}");
        return;
    }
    
    try {
        oldStorage = JSON.parse(oldStorage);
        if (typeof options == 'object') {
            for(let name in options) {
                oldStorage[name] = options[name]
            }
        }
        newStorage = oldStorage;
        currentStorage.setItem(name, JSON.stringify(newStorage));
        return;
    }
    catch(err) {
        console.log(err)
    }

    currentStorage.setItem(name, options);
    return;
 }

 const getStorageFun = (name, options, storage) => {
    let oldStorage = storage.getItem(name);
    let newStorage;
    if (!options) {
        try {
            newStorage = JSON.parse(oldStorage)
            if (typeof newStorage == 'object') {
                return newStorage
            }
        } catch (err){
            console.log(err);
        }
        return oldStorage;
    }
    // 不存在
    if (!oldStorage) {
        return '';
    }

    // 复合类型
    if (typeof options == 'object') {  // instanceof
        console.log('复合类型')
        // let obj = {};
        // ['name', 'age']
        
        try {
            newStorage = JSON.parse(oldStorage);
            if (typeof newStorage == 'object') {
                const obj = options.reduce((obj, c) => {
                    // const temp = newStorage[c];
                    obj[c] = newStorage[c];
                    return obj;
                }, {})

              return obj;
            }
        } catch (err) {
            console.log(err);
        }

    }

    // 基本类型
    try {
        newStorage = JSON.parse(oldStorage)
        if (typeof newStorage == 'object') {
            return newStorage[options]
        }
    } catch (err){
        console.log(err);
    }
    return oldStorage;
 }

 const setStorage = (name, options) => {
    setStorageFun(name, options, 'localStorage') 
 }

 const getStorage = (name, options) => {
    return getStorageFun(name, options, localStorage)
 }

const setSessionStorage = (name, options) => {
    setStorageFun(name, options, sessionStorage);
}

const getSessionStorage = (name, options) => {
    return getStorageFun(name, options, sessionStorage);
}

export {
    setStorage,
    getStorage,
    setSessionStorage,
    getSessionStorage,
}

// export default {
//     stoarge: {

//     }
// }

 